/*
 * $Id$
 *
 * Copyright ( c ) 2014 TH Supcom Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of TH Supcom
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with TH Supcom Corporation or a TH Supcom
 * authorized reseller (the "License Agreement"). TH Supcom may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, TH Supcom DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. TH Supcom DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * TH Supcom SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF TH Supcom HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. TH Supcom' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE TH Supcom SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.impl.common.PhamApplyConstant;
import com.th.supcom.pds.impl.common.enums.AvailableEnum;
import com.th.supcom.pds.impl.common.enums.NeedDispenseEnum;
import com.th.supcom.pds.impl.common.enums.NoDispenseReason;

/**
 * 计算患者费用是否充足
 */
public final class CheckAdequatePrepayment
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (CheckAdequatePrepayment.class);

    private CheckAdequatePrepayment ()
    {

    }

    public static void isPrepayEnough (Map <Long, List <InpPhamApplyDetailVO>> detailMap, List <InpPhamApplyPatInfo> currentPatInfos)
    {
        Map <Long, InpPhamApplyPatInfo> patMap = PhamApplyConvertUtils.convertPatInfoListToMapByPaivisitId (currentPatInfos);
        Set <Map.Entry <Long, List <InpPhamApplyDetailVO>>> set = detailMap.entrySet ();
        for (Entry <Long, List <InpPhamApplyDetailVO>> entry : set)
        {
            List <InpPhamApplyDetailVO> patDetailList = entry.getValue ();
            double patTotalCost = 0;
            for (InpPhamApplyDetailVO inpPhamApplyDetailVO : patDetailList)
            {
                patTotalCost += inpPhamApplyDetailVO.getCosts ();
            }
            InpPhamApplyPatInfo patInfo = patMap.get (entry.getKey ());
            // 如果取不到预交金，默认为0
            double prePayment = 0l;

            if (null == patInfo)
            {
                continue;
            }

            if (null != patInfo.getAvailablePrepayment ())
            {
                prePayment = patInfo.getAvailablePrepayment ();
            }
            boolean isSpecialChargeType = (patInfo.getChargeType ().equals (PhamApplyConstant.SPECIALCHARGETYPE));
            if (patTotalCost <= prePayment)
            {
                setDispenseResultFlag (patDetailList, false, isSpecialChargeType);
                continue;
            }
            else
            {
                setDispenseResultFlag (patDetailList, true, isSpecialChargeType);
                continue;
            }
        }
    }

    /**
     * 给定是否欠费摆药
     * 
     * @param patDetailList 请领明细
     * @param mandatoryDispenseFlag 强制摆药标志
     * @param arreas 是否欠费
     * @param isSpecialChargeType 是否为特殊费用类型
     */
    public static void setDispenseResultFlag (List <InpPhamApplyDetailVO> patDetailList, boolean arreas, boolean isSpecialChargeType)
    {
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : patDetailList)
        {
            // 如果是剩余数量足够了不摆药则不摆药
            if (NoDispenseReason.LEFT_ENOUGH.getEnumName ().equals (inpPhamApplyDetailVO.getNoDispenseMemo ()))
            {
                continue;
            }

            // 如果药品不可供则不摆药
            if (AvailableEnum.NO.getEnumCode ().equals (inpPhamApplyDetailVO.getApplySupplyFlag ()))
            {
                inpPhamApplyDetailVO.setDispenseResultFlag (NeedDispenseEnum.NO.getEnumCode ());
                inpPhamApplyDetailVO.setNoDispenseMemo (NoDispenseReason.NO_SUPPLY.getEnumName ());
                continue;
            }

            String obligeFlag = null;
            boolean noArreas = !arreas;
            // 如果是强制摆药或者没有欠费则摆药
            if (noArreas || NeedDispenseEnum.FORCE.getEnumCode ().equals (inpPhamApplyDetailVO.getDispenseResultFlag ()))
            {
                obligeFlag = AvailableEnum.YES.getEnumCode ();
            }
            // 如果是特殊费用类型或者欠费摆药
            else if (isSpecialChargeType || AvailableEnum.YES.getEnumCode ().equals (inpPhamApplyDetailVO.getMandatoryDispenseFlag ()))
            {
                obligeFlag = AvailableEnum.YES.getEnumCode ();
            }
            else
            {
                obligeFlag = AvailableEnum.NO.getEnumCode ();
            }

            if (arreas)
            {
                inpPhamApplyDetailVO.setNoDispenseMemo (NoDispenseReason.LACK_MONEY.getEnumName ());
            }

            if (AvailableEnum.NO.getEnumCode ().equals (inpPhamApplyDetailVO.getStockEnoughFlag ()))
            {
                inpPhamApplyDetailVO.setNoDispenseMemo (NoDispenseReason.LACK_STOCK.getEnumName ());
            }

            inpPhamApplyDetailVO.setDispenseResultFlag (obligeFlag);
            inpPhamApplyDetailVO.setArreas (arreas);
        }
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
